//
//  BaseNavigationViewController.m
//  LYPhotos
//
//  Created by 美鑫科技 on 16/5/5.
//  Copyright © 2016年 MorpLCP. All rights reserved.
//

#import "BaseNavigationViewController.h"

@interface BaseNavigationViewController ()

@property (nonatomic, strong) Theme *theme;

@end

@implementation BaseNavigationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initTheme];
}

#pragma mark ---------------- 主题设置相关 --------------------------
// 初始化主题
- (void)initTheme{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(themeDidChanged:) name:@"themeChange" object:nil];
    for (UIView *vc in self.navigationBar.subviews) {
        if ([vc isKindOfClass:NSClassFromString(@"_UINavigationBarBackground")]) {
            [vc removeFromSuperview];
        }
    }
    self.lyBar = [LYNavigationBar navigationBarWithTintColor:[ThemeManager shareManager].theme.themeColor];
    self.theme = [ThemeManager shareManager].theme;
    [self.navigationBar insertSubview:self.lyBar atIndex:0];
}

- (void)setTheme:(Theme *)theme{
    _theme = theme;
    if (theme.themeColor) {
        self.lyBar.backgroundColor = theme.themeColor;
        [self.lyBar setBackgroundImage:nil];
    }
    if (theme.themeImage) {
        [self.lyBar setBackgroundImage:theme.themeImage];
    }
    if (theme.themeImageURL) {
         [self.lyBar setBackgroundImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:theme.themeImageURL]]]];
    }
    if (theme.themeTextColor) {
        self.navigationBar.tintColor = theme.themeTextColor;
        self.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:theme.themeTextColor};
    }
}

- (void)setBarStyle:(UIStatusBarStyle)barStyle{
    _barStyle = barStyle;
    [self preferredStatusBarStyle];
}

- (UIStatusBarStyle)preferredStatusBarStyle{
    return self.barStyle;
}

- (void)themeDidChanged:(NSNotification *)notifi{
    self.theme = [ThemeManager shareManager].theme;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
